﻿namespace PassiveView
{
    public class RegistrationController
    {
        private readonly IRegistrationService _service;
        private readonly IRegistrationView _view;

        public RegistrationController(IRegistrationView view, IRegistrationService service)
        {
            _view = view;
            _service = service;
        }

        public void ValidateForm()
        {
            _view.ValidationErrors.Clear();
            _view.Enable();

            if (string.IsNullOrEmpty(_view.Email))
                ReportProblem("Email cannot be blank.");

            if (string.IsNullOrEmpty(_view.Password))
                ReportProblem("Password cannot be blank.");

            if (_view.Password != _view.Confirm)
                ReportProblem("Password and confirmation do not match.");
        }

        public void Register()
        {
            _service.Register(_view.Email, _view.Password);
        }

        private void ReportProblem(string reason)
        {
            _view.Disable();
            _view.ValidationErrors.Add(reason);
        }
    }
}